<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-2-2
 * Time: 下午2:07
 */
class file extends  MY_Portal_Controller
{
    function __construct(){
        parent::__construct();
        $this->_login_user = $this->session->userdata('login_user');
    }

    function index(){
        echo uri_string().'<br/>';
        echo current_url().'<br/>';
        echo uri_string().'<br/>';
    }

    function upload(){
        $this->auth();
        $this->assign('css_load',array('index','upload','bootstrap'));
        $this->assign('head_title','文件上传');
        $this->assign('file_upload_url',base_url().'static/FileUpload/');
        $this->load->model('classify/classify_model');
        $classify = $this->classify_model->select_classify_by_user($this->_login_user['user_id']);
        $this->assign('classify',$classify);
        $this->display();
    }

    function do_upload(){
        $this->config->load('upload');
        $config = $this->config->item('file_url');
        $config['user_id']  = $this->_login_user['user_id'];
        $this->load->library('File_upload',$config);
    }
    /*
     * 确认上传
     * method:post
     * param:
     *       file_info:json
     */
    function sure_upload(){
        $json = $this->input->post('files');
        $files = json_decode($json,true);
        $this->load->model('public/file_uploads');
        $err = array();
        foreach($files as $file){
            $file['user_id'] = $this->_login_user['user_id'];
            echo $file['file_path'] = urlencode($this->_login_user['user_id'].'/'.$file['title']);

            $file['file_type'] = $this->_get_suffix($file['title']);
            $file_path = $this->_get_path_($file['title']);
            $pdf_path = iconv('utf-8','gbk',$this->_change_suffix($file_path,'pdf'));
            $file['title'] = $this->_del_suffix($file['title']);
            if(file_exists($pdf_path)){
                 $file['create_pdf'] = 1;
                echo 'bavas';
            }
            $result = $this->file_uploads->save($file);
            if(!$result){
                array_push($err,$file);
            }
        }
        if(count($err)){
            $this->_json(array('state'=>false,'mesg'=>$err));
        }
        $this->_json(array('state'=>true));
    }


    function do_del($file_id){
        $this->load->model('public/file_base');
        $file = $this->file_base->get_file_by_id($file_id);
        $file_path = $file['file_path'];
        $real_file_name = iconv('utf-8','gbk',urldecode($this->_get_name($file_path)));
        $swf_file_name = $this->_change_suffix($real_file_name,'swf');
        $pdf_file_name = $this->_change_suffix($real_file_name,'pdf');
        $swf_path = $this->_get_path_($swf_file_name);
        $pdf_file_path = $this->_get_path_($pdf_file_name);
        $file_path = $this->_get_path_($real_file_name);
        @unlink ($swf_path);
        @unlink ($pdf_file_path);
        @unlink ($file_path);
        $this->file_base->del($file_id);
        $this->_json(array('state'=>true));
    }

    function show($file_id = 0,$classify_id = 0){
        $this->_layout = '';
        $this->load->model('public/file_base');
        $file = $this->file_base->get_file_by_id($file_id);
        if(!$file['create_pdf']){
            exit('404,not found the file!');
        }
        $config = array(
            'source_charset'=>'utf-8',
            'target_charset'=>'utf-8',
            'load_all'  =>  false
        );
        $this->load->library('phpanalysis',$config);
        $this->phpanalysis->SetSource($file['title']);
        $this->phpanalysis->differMax = false;
        $this->phpanalysis->unitWord = false;
        $this->phpanalysis->StartAnalysis( false );
        $result = $this->phpanalysis->GetFinallyResult(' ', true);
        $result = explode(' ',$result);
        $recommend = array();
        $j=count($recommend);
        $not_in = array();
        $not_in[0] =$file['id'];
        $i=1;
        foreach($result as $v){
            if($v!="")
            {
                $back = $this->file_base->get_like($v,$not_in);
                if(is_array($back)){
                    $recommend[$j]=$back;
                    foreach($back as $v)
                    {
                        $not_in[$i] = $v['id'];
                        $i++;
                    }
                     $j++;
                }

            }
        }

        $tag = explode(',',$file['tag']);
        foreach($tag as $value){
            $back = $this->file_base->get_tag_like($value,$not_in);
            if(!is_null($back)){
                $recommend[$j]=$back;
                foreach($back as $v)
                {
                    $not_in[$i] = $v['id'];
                    $i++;
                }
                $j++;
            }
        }
        urldecode($this->_get_pdf_name($file['file_path']));
        $pdf_path = urldecode($this->_get_pdf_name($file['file_path']));
        $this->assign('pdf_path',$pdf_path);
        $this->assign('recommend',$recommend);
        $this->assign('file',$file);
        $this->display();
    }

    function do_down(){
        $this->auth();
        $this->load->model('classify/classify_model');
        $classify = $this->classify_model->select_classify_by_user($this->_login_user['user_id']);
        var_dump($classify);
    }
    /*
     * 将文件加入打印序列
     * post: file_id,num
     */

    function do_print(){
        $user_id = $this->_login_user['user_id'];
        if(!$user_id){
            $this->_json(array('state'=>false));
        }


        $file_id = $this->input->post('file_id');
        $file_num = $this->input->post('file_num');

        $files = array('file_id'=>$file_id,'file_num'=>$file_num);
        $cart = $this->session->userdata('cart');
        $arr_file = json_decode($cart['file_info'],true);
        $count = count($arr_file);
        if($count<1){
            $file_info = array(0=>$files);
        }else{
            $file_info[$count] = $files;
        }
        $file_info = json_encode($file_info);
        $cart = array(
            'user_id' => $user_id,
            'file_info' => $file_info
        );

        $cart = $this->get_cart($user_id);


        if($this->_login_user){
            $this->load->model('user/user_model');
            $this->user_model->set_cart($user_id,$file_info);
        }
        $this->session->set_userdata('cart',$cart);
    }

    function do_change_cart(){
        $this->auth();
        $file_id = $this->input->post('file_id');
        $file_num = $this->input->post('file_num');
        $title = $this->input->post('title');
        $file_info = $this->session->userdata('cart');
        $file_array = array();
        //var_dump($file_info);
        if($file_info)
        {
            $file_info = json_decode($file_info,true);
            $flag = 0;
            $i = 0;
            foreach($file_info as $file){
                if($file['id']==$file_id){
                    $file_array[$i]['file_num'] = $file['file_num'] + $file_num;
                    $flag=1;
                }else{
                    $file_array[$i]['file_num'] = $file['file_num'];
                }
                $file_array[$i]['id'] = $file['id'];
                $file_array[$i]['title'] = $file['title'];
                $i++;
            }
            if(!$flag){
                $count = count($file_info);
                $file_array[$count]=array('id'=>$file_id,'file_num'=>$file_num,'title'=>$title);
            }
        }else{
            $file_array[0]=array('id'=>$file_id,'file_num'=>$file_num,'title'=>$title);
        }
        //var_dump($file_array);
        $file_info = json_encode($file_array);
        $this->session->set_userdata('cart',$file_info);
        $this->_json(array('state'=>true));
    }
    /*
     * 打印列表,选择商家，提交打印
     */
    function cart(){

        $file_info = $this->session->userdata('cart');
        $file_info = json_decode($file_info,true);
        $this->load->model('public/public_company');
      //$companies = $this->public_company->entry_company();  //这里可以让商家选择区域
        $companies = $this->public_company->get_company();
        $this->assign('company',$companies);
        $this->assign('file_info',$file_info);
        $this->assign('head_title','我的打印');
        $this->display();
    }
    /*
     * 下单打印
     * post json
     */
    function do_place(){
        $this->auth();
        $order_info =array();
      //  $files = array();
        $file_info = trim($this->input->post('file_info'));
        if(!$file_info){
            $file_info = $this->session->userdata('cart');
        }
        //ajax post json  ->  decode to array
        // or session  $this->session->userdata('cart')
        //explame:   {0:{'id':2,'num':1},1:{'id':3,'num':4}}
        $file_info = json_decode($file_info,true);
        $i = 0;
        $sum = 0;
        foreach($file_info as $file){
        //    $files['file'][$i]['id'] = $file['id'];
        //    $files['file'][$i]['file_num'] = $file['file_num'];
            $sum +=   $file['file_num'];
            $i++;
        }
        $order_info['company_id'] = $this->input->post('company_id');
        $order_info['user_id'] = $this->_login_user['user_id'];
        $order_info['info'] = json_encode($file_info);
        $order_info['side'] = $this->input->post('side')?$this->input->post('side'):1;
        $order_info['remarks'] =  $this->input->post('remarks')?$this->input->post('remarks'):'';
        $order_info['sum_num'] = $sum;
        $this->load->helper('verify');
        $order_info['order_id'] = 'xb'.rand_pass(10).date("YmdHiS",time());

        $this->load->model('user/order');
        $result = $this->order->insert_order($order_info);
       // echo $this->order->last_query();
        if(!$result){
            $this->_json(array('state'=>false));
        }
        $this->_json(array('state'=>true));
    }

    private function _get_name($file_path){
        $file_name = end(explode("/",$file_path));
        return $file_name;
    }
    private function _get_swf_name($file_name){
        $name = $this->_change_suffix($file_name,'swf');
        return $name;
    }
    private function _get_pdf_name($file_name){
        $name = $this->_change_suffix($file_name,'pdf');
        return $name;
    }
    private function _get_path_($file_name){
        // $file_name = $this->_get_path($file_name);
        return $_SERVER['DOCUMENT_ROOT'].'/data/uploads/'.$this->_login_user['user_id'].'/'.$file_name;
    }/**/

    private function _change_suffix($file_name,$suffix = 'pdf'){
        $name = $this->_del_suffix($file_name);
        return $name.'.'.$suffix;
    }
    private function _del_suffix($file_name){
        $arr = explode('.',$file_name);
        $name ='';
        for($i=0;$i<count($arr)-1;$i++){
            if($i!=0){
                $name .= '.'.$arr[$i];
            }else{
                $name .= $arr[$i];
            }
        }
        return $name;
    }
    private function _get_suffix($file_name){
        return end(explode('.',$file_name));
    }

    private function _office_to_pdf($file_path) {
        $file_path = (iconv("UTF-8","gbk",$file_path));
        header("Content-type:text/html; charset=utf-8");
        if (substr(php_uname(), 0, 7) == "Windows"){
            echo $cmd = "C:/jod.bat ".$file_path;
            pclose(popen($cmd,'r'));
            //exec($cmd);
        }
        else {
            $cmd = "java -jar /alidata/www/f/jod5.jar ".$file_path;
            exec($cmd . " > /dev/null &");
        }
    }
}